package org.brisling.docManage.service;

import java.util.List;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.docManage.domain.Docdetail;
import org.brisling.docManage.queryService.QueryDocService;
import org.brisling.docManage.repository.DocdetailRepo;
import org.brisling.sysman.domain.SysLob;
import org.brisling.sysman.service.LobService;
import org.brisling.sysman.service.UploadService;
import org.brisling.sysman.service.base.BaseSysmanTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/docdetail")
/**
 * 文档明细服務service
 * @Author : jackson wang
 * @Date   : 2015年6月8日 下午4:18:01
 * @version: 1.0
 */
public class DocdetailService extends BaseSysmanTypeService<Docdetail>{
	
	@SuppressWarnings("unused")
	@Autowired
	private DocdetailRepo baseRepo;
	@Autowired
	private LobService lobService;
	@Autowired
	private QueryDocService queryDocService;
	
	
	@Autowired
	private UploadService 	uploadService;
	
	public DocdetailService(){
		this.moduleName = ConstSysParam.MODULE_DOCDETAIL;
		this.clazz = Docdetail.class;
	}
	
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return new ModelAndView("/document/docdetailList");
	}
	
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/deptMainPage.action")
	public ModelAndView getDeptMainPage(){
		return new ModelAndView("/document/docDeptDetailList");
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return getBizEditPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/modulePage.action")
	public ModelAndView getModulePage(){
		return getBizEditPage();
	}
	
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getListDomain.action")
	public ListDomain<Docdetail> getListDomain(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
		
		return super.getPagedList(page, rows);
			
	}
	
	/**
	 * 分页查询文档明细列表页面
	 * @param page
	 * @param rows
	 * @param typeId
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getTypeListDomain.action")
	public ListDomain<Docdetail> getTypedListDomain(
			@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="typeid",required=false) Long typeId,			
			@RequestParam(value="sort",required=false) String sort){		
		
		
		return super.getPagedListDomainByTypeId(typeId, page, rows);
			
	}
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getList.action")
	public List<Docdetail> getPagedListRows(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
		
			return super.getAll(page,rows,getSort(sort));
			
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/del.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
				
		return super.delRows(id, ids);		 
	}
	
	
	/**
	 * 保存文档内容
	 * @param id
	 */
	@RequestMapping("/save.action")
	public Docdetail save(@RequestBody String body){
		Docdetail beforeSavedDomain = bodyToObj(body, Docdetail.class);
		
		
		Long lobid = null;
		Docdetail afterSavedDomain = null;
		if(beforeSavedDomain.getId()==null){
			//新增操作，执行保存，获取id值
			afterSavedDomain = super.save(beforeSavedDomain);
			
			
			//更新附件，将临时id(tmpid)替换为保存后的id值
			if(beforeSavedDomain.getTmpid()!=null && beforeSavedDomain.getTmpid().trim().length()>0){
				uploadService.updateTmpid(beforeSavedDomain.getBizcode(),"'"+beforeSavedDomain.getTmpid()+"'",afterSavedDomain.getId().toString());				
			}
			
		}else{
			afterSavedDomain = beforeSavedDomain;			
		}
		
		lobid = afterSavedDomain.getId();
		
		beforeSavedDomain.setId(lobid);
		if(beforeSavedDomain.getContents()!=null || beforeSavedDomain.getLob_contents()!=null){
			SysLob savedLob = lobService.save(beforeSavedDomain);
			String _lobid = Long.toString(savedLob.getId());
			afterSavedDomain.setLobid(_lobid);
			
			afterSavedDomain.setContents(savedLob.getContents());
			afterSavedDomain.setLob_contents(savedLob.getLob_contents());
		}
		
		if(afterSavedDomain.getSortedid()==null){
			afterSavedDomain.setSortedid(lobid.intValue());
		}
		return super.save(afterSavedDomain);
	}
	
	/**
	 * 批量审核
	 * @param ids
	 * @param status
	 * @return
	 */
	@RequestMapping("/auditList.action")
	public RetDomain setAuditStatusList(@RequestParam(value="ids",required=false) String ids,@RequestParam(value="status",required=false)String status){
		
		RetDomain domain  = new RetDomain();
		if( (ids!=null) && (status !=null)){
			 int num = super.updateAuditStatusList(ids, status);
			 domain.setSuccessMsg(num+"");
		}else{
			 domain.setErrorMsg("error");
		}
		
		return domain;
	}
	
	/**
	 * 单条记录审核
	 * @param id
	 * @param status
	 * @return
	 */
	@RequestMapping("/auditOne.action")
	public Docdetail setAuditStatus(@RequestParam(value="id",required=false) Long id,@RequestParam(value="status",required=false) String status){
		String ret = null;
		if( (id!=null) && (status !=null)){
			ret =super.updateAuditStatus(id, status);
		}
		if(ret!=null && ret=="200"){
			return queryDocService.getOne(id);
		}
		return null;
	}
	
	/**
	 * 更新点击量
	 * @param id
	 * @return
	 */
	@RequestMapping("/clickCnt.action")
	public RetDomain updateCnt(@RequestParam(value="id",required=false) Long id){
		
		RetDomain r = new RetDomain();
		Docdetail d = this.getOne(id);
		if(d!=null){
			Integer cnt = d.getClick_count();
			cnt = cnt ==null?1:cnt++;			
			d.setClick_count(cnt);
			this.save(d);
		}else{
			r.setErrorMsg("数据异常，更新点击量失败");
		}
		
		return r;
	}
	
	/**
	 * 更新点击量
	 * @param id
	 * @return
	 */
	public RetDomain updateCnt(Long id,Integer cnt){
		
		RetDomain r = new RetDomain();
		Docdetail d = this.getOne(id);
		if(d!=null){			
			cnt = cnt ==null?1:cnt++;	
			Integer c = d.getClick_count();
			c = c==null?1:c;
			cnt = cnt >c?cnt:c;
			d.setClick_count(cnt);
			this.save(d);
		}else{
			r.setErrorMsg("数据异常，更新点击量失败");
		}
		
		return r;
	}
}
